package clusteringTest;


import java.io.IOException;
import java.io.StringReader;
import java.util.Iterator;
import java.util.ArrayList;
import java.util.List;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.TokenStream;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.analysis.tokenattributes.TermAttribute;
import org.apache.lucene.util.Version;
import org.apache.mahout.math.RandomAccessSparseVector;
import org.apache.mahout.math.SequentialAccessSparseVector;
import org.apache.mahout.math.Vector;
import org.apache.mahout.vectorizer.encoders.FeatureVectorEncoder;
import org.apache.mahout.vectorizer.encoders.StaticWordValueEncoder;

public class VectorizingText {

	public RemoveStopWord rw = new RemoveStopWord();
	
	public SequentialAccessSparseVector TransformToVector(String text) throws IOException {
		FeatureVectorEncoder encoder = new StaticWordValueEncoder("text");//Feature hashing with Mahout APIs 
//		Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_31);   
		
        String content = rw.RemoveStop(text);
        System.out.println("remove stopword: " + content);//remove stop word (include english and chinese)
        
		String[] terms = content.split(" ");
		Vector v1 = new SequentialAccessSparseVector(5000);  
		for(int i = 0;i<terms.length;i++)
		{
			 String w = terms[i];
//			 System.out.println("Splited Word:  "+ w);
			 encoder.addToVector(w, 1, v1);   
 //   		 System.out.println("one word end");
		}
		SequentialAccessSparseVector sv = new SequentialAccessSparseVector(v1);
		return sv;
	}
	
	/*
	public static List<Vector> test() throws IOException
	{
		TokenizingAndVectorizingText test =new TokenizingAndVectorizingText();
		Vector v1 = test.TransformToVector("text to magically vectorize name text");// name text
		Vector v2 = test.TransformToVector("text to magically vectorize name text");
		Vector v3 = test.TransformToVector("machine learning");
		//tvt.TransformToVector(v1);
		List<Vector> vec = new ArrayList<Vector>();
		vec.add(v1);
		vec.add(v2);
		vec.add(v3);
		return vec;
	}
	*/
	

	public static void main(String args[]) throws IOException
	{
		VectorizingText test =new VectorizingText();
		Vector v1 = test.TransformToVector("开启 邮箱 开启 一天 的 愉快");// name text
		Vector v2 = test.TransformToVector("text to magically vectorize name text");
		Vector v3 = test.TransformToVector("machine learning");
//		List<Vector> vec = new ArrayList<Vector>();
//		vec.add(v1);
//		vec.add(v2);
//		vec.add(v3);
		

		System.out.println(v1.toString());
		System.out.println(v2.toString());
		System.out.println(v3.toString());
		
	}


}

